﻿@{
    ViewBag.Title = "任务管理";
    Layout = "~/Views/Shared/_indexBase3.cshtml";
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery-2.0.3.js"></script>
        <script src="~/LiberUI/Scripts/jquery.ba-resize.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Controllers.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/SiteUI/BaseScripts/site.js"></script>
    }
    <link href="~/SiteUI/Base.css" rel="stylesheet" />
    <link href="~/SiteUI/Task/Task.css" rel="stylesheet" />
    <link href="~/SiteUI/BaseScripts/pikaday.css" rel="stylesheet" />
}
@section HeadCnt{
    <div class="MiddleWraper">
        <h2 class="index_Title">@ViewBag.Title</h2>
        <a class="SubIndex" href="/Task/Index">我的任务</a>
        <a class="SubIndex" href="#">看板</a>
        <a class="SubIndex active" href="/Task/Manage">任务管理</a>
    </div>
}

@section HeadBar{

}

@section LeftNavi{
    <div id="DeptCard">
        <h3 id="DeptName">雅/碧/美运维团队</h3>
    </div>
    <div id="lstMembers">

    </div>
}
<div id="ViewArea">
    <div id="lstMemberTasks">

    </div>
</div>


@section RightSlide{
    <div class="InfoBlock">
        <span class="BlockTitle">我关注的任务</span>
        <div id="lstMyFocus">

        </div>
    </div>
}

<div id="tmp">
    @RenderPage("/SiteUI/wgBase.cshtml")

    <div class="TaskCharger memberCard">
        <img src="/SiteUI/Imgs/defAvataNormal.gif" />
        <div class="UserInfo">
            <h4></h4>
            <span>延误：</span><span class="deferredTaskCount">-</span><i class="alertTooMuch"></i>
            <span>待办：</span><span class="activeTaskCount">-</span><i class="alertTooMuch"></i>
        </div>
    </div>
</div>

<script>
    Env.tplMemberCard = $(".memberCard").eq(0);
    Env.CItem = null;
    Env.LoadedTask = 0;
    Env.CPage = 0;
    Env.HasMore = false;
    Env.Model = new TaskModel();

    function MakeMemberCard(u) {
        //构造人员列表卡片
        var $crd = Env.tplMemberCard.clone();
        $crd.attr('IDStr', u.AcntId);
        $crd.children("img").attr('src', u.AvataPath ? u.AvataPath : '/SiteUI/Imgs/defAvataNormal.gif');
        $crd.children(".UserInfo").children("h4").text(u.UserName);
        var dly = $crd.find(".deferredTaskCount").text(u.Counting.DelyCount)
        if (u.Counting.DelyCount > 0) {
            dly.addClass('delay');
        }

        if (!u.Counting.Active) {
            u.Counting.Active = 0;
        }
        
        var todo = $crd.find(".activeTaskCount").text(u.Counting.Active);
        return $crd;
    };

    function MakeTaskView(tsk) {
        //TODO:构造管理界面的任务卡片
        Env.Model.Bind(tsk);
        var $crd = $("<div>").addClass('TaskCard').addClass(tsk.Status);
        var $head = $("<div class='NormalFields'>");
        $crd.append($head);

        $head.append($("<span>").text(tsk.SubSystem));
        $head.append($("<span>").text(tsk.Type));
        var dl = $("<span>").addClass('DelayInfo').text(tsk.Deadline.split(' ')[0]);
        if (tsk.Delay > 0) {
            dl.addClass('deferred');
        }
        $head.append(dl);
        $head.append($("<span>").text(Env.Model.GetFriendlyValue("Status")));
        $head.append(new Rating(null, { ShowDescribe: false }).val(tsk.Importance).disable().dom());
        
        $crd.append($("<h5>").text(tsk.Content));
        $crd.append($("<span>").addClass('crdProposeInfo').text(tsk.Proposer + ' ' + tsk.ProposeTime.split(' ')[0]));
        return $crd.fadeIn();
    };

    function LoadMembers(rtn) {
        if (rtn.IsPositive) {
            Env.lstMembers.Clear().Append(rtn.Data.MembersCounting);
        }
    };

    function LoadMemberDtl(ev) {
        Env.CItem = ev;
        Env.CPage = 0;
        Env.lstTasks.dom($("#tmp"));
        $("#ViewArea").html('').append(Env.aniLoading.clone().show());
        Env.lstTasks.Clear();

        new AjaxHelper('get', '/Task/UserTodo', { uid: Env.CItem.Data.AcntIdStr }, LoadDtlEnd,Env.CItem.Data.AcntIdStr);
        new AjaxHelper('get', '/Task/UserTask', { uid: Env.CItem.Data.AcntIdStr, page: Env.CPage }, LoadDtlEnd, Env.CItem.Data.AcntIdStr);

        //TODO:获取基本统计信息
        //TODO:获取统计信息改为与获取人员列表后的独立步骤，则这里仅需要加载即可
    };

    function LoadDtlEnd(rtn, acntId) {
        $("#ViewArea").html('').append(Env.lstTasks.dom());
        if (rtn.IsPositive && acntId == Env.CItem.Data.AcntIdStr) {
            Env.lstTasks.Append(rtn.Data.Tasks);
            Env.LoadedTask = Env.LoadedTask + rtn.Data.Tasks.length;
            if (rtn.Data.Sum && rtn.Data.Sum > Env.LoadedTask) {
                Env.HasMore = true;
                Env.scl.on('getBottom', LoadMoreTask);
            }
            else {
                Env.HasMore = false;
                Env.scl.off('getBottom', LoadMoreTask);
            }
        }
    };

    function LoadMoreTask() {
        Env.CPage = Env.CPage + 1;
        new AjaxHelper('get', '/Task/UserTask', { uid: Env.CItem.Data.AcntIdStr, page: Env.CPage }, LoadDtlEnd, Env.CItem.Data.AcntIdStr);
    };

    function GoTaskDetail(ev) {
        window.open("/Task/Detail?id=" + ev.Data.IDStr);
    };

    $(function () {
        //获取团队成员列表及每个人的粗略任务统计
        Env.lstMembers = new List($("#lstMembers"), { StyleForHot: ' ',Selectble:true});
        Env.lstMembers.af_makeView = MakeMemberCard;

        //加载团队成员
        new AjaxHelper('get', '/Task/GetDeptTaskCount', null, LoadMembers);

        Env.lstMembers.on('itemClick', LoadMemberDtl);
        Env.lstTasks = new List($("#lstMemberTasks"), { StyleForHot: '', Selectble: false });
        Env.lstTasks.af_makeView = MakeTaskView;
        Env.lstTasks.on('itemDBClick', GoTaskDetail);
    });
</script>

